package com.zysc.processflow.common.base;

import org.springframework.lang.Nullable;

/**
 * 返回结果类
 */
public class ResultBody {

        public ResultBody() {
        }
        public ResultBody(Object o) {
            this.code = 200;
            this.message = "success";
            this.data = o;
        }

        public ResultBody(int code, String message, Object o) {
            this.code = code;
            this.message = message;
            this.data = o;
        }

        /**
         * 响应码
         */
        private int code;
        /**
         * 响应信息
         */
        private String message;

        /**
         * 响应数据
         */
        private Object data;


        /**
         * 数据总数
         */
        private Long total;


        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public Object getData() {
            return data;
        }

        public void setData(Object data) {
            this.data = data;
        }

        public Long getTotal() {
            return total;
        }

        public void setTotal(Long total) {
            this.total = total;
        }


        public static ResultBody Success(Object result, @Nullable Long total) {
            ResultBody data = new ResultBody();
            data.setCode(200);
            data.setData(result);
            if (total != null) {
                data.setTotal(total);
            }
            return data;
        }

        public static ResultBody Success(Object result) {
            ResultBody data = new ResultBody();
            data.setCode(200);
            data.setData(result);
            return data;
        }

        public static ResultBody Success(Object result, @Nullable String msg) {
            ResultBody data = new ResultBody();
            data.setCode(200);
            data.setData(result);
            if (msg != null || msg.equals("")) {
                data.setMessage(msg);
            }
            return data;
        }


        public static ResultBody Success(Object result, @Nullable String msg, @Nullable Long total) {
            ResultBody data = new ResultBody();
            data.setCode(200);
            data.setData(result);
            if (msg != null) {
                data.setMessage(msg);
            }
            if (total != null) {
                data.setTotal(total);
            }
            return data;
        }

        public static ResultBody error(String errormsg, @Nullable Integer code) {
            ResultBody data = new ResultBody();
            if (code == null) code = 999;
            data.setCode(code);
            data.setMessage(errormsg);
            return data;
        }

        public static ResultBody error(String errormsg, @Nullable Integer code, @Nullable Object rs) {
            ResultBody data = new ResultBody();
            if (code == null) code = 999;
            data.setCode(code);
            if(rs!=null){
                data.setData(rs);
            }
            data.setMessage(errormsg);
            return data;
        }
}
